package tk.mybatis.springboot.service;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.springboot.dto.CountryDto;
import tk.mybatis.springboot.mapper.CountryMapper;

import java.util.List;

/**
 *
 *
 */
@Service
public class CountryService {

    @Autowired
    private CountryMapper countryMapper;


    public List<CountryDto> query(CountryDto country) {
        if (country.getPageNo() != null && country.getPageSize() != null) {
            PageHelper.startPage(country.getPageNo(), country.getPageSize());
        }
        Example example = new Example(CountryDto.class);
        Example.Criteria criteria = example.createCriteria();
        /*
        //需coding查询条件
        if (country.getCountryname() != null && country.getCountryname().length() > 0) {
            criteria.andLike("countryname", "%" + country.getCountryname() + "%");
        }
        if (country.getCountrycode() != null && country.getCountrycode().length() > 0) {
            criteria.andLike("countrycode", "%" + country.getCountrycode() + "%");
        }
        */
        return countryMapper.selectByExample(example);
    }


    public CountryDto getById(String id) {
        return countryMapper.selectByPrimaryKey(id);
    }

    public void deleteById(String id) {
        countryMapper.deleteByPrimaryKey(id);
    }

    public CountryDto save(CountryDto country) {
        countryMapper.insert(country);
        return country;
    }

    public void updateById(CountryDto country) {
        countryMapper.updateByPrimaryKey(country);
    }

    public void updateIgnoreNullById(CountryDto country) {
        countryMapper.updateByPrimaryKeySelective(country);
    }


}
